package com.wxapp.common.util.reflect;

import com.wxapp.common.base.util.bean.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectUtil {
    private static final Logger log = LoggerFactory.getLogger(ReflectUtil.class);
    private ReflectUtil() {
    }
    public static Object getFieldValue(Object obj, String fieldName) {
        try {
            Field field = obj.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return getField(field,obj);
        } catch (Exception e) {
            log.error("反射获取类失败", e);
            throw new RuntimeException(e);
        }
    }


    public static Object getField(Field field, Object target) {
        try {
            return field.get(target);
        } catch (Exception e) {
            log.error("反射获取类失败", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * getMethod
     * @return
     */
    public static Method getMethod(String key,Class aclass) {
        try {
           return aclass.getMethod(key);
        } catch (Exception e) {
            log.error("反射获取类失败", e);
            throw new RuntimeException(e);
        }
    }
    public static Object invokeMethod(Method method, Object target,@Nullable Object... args) {
        try {
            return method.invoke(target,args);
        } catch (Exception e) {
            log.error("反射获取类失败", e);
            throw new RuntimeException(e);
        }
    }
}
